Thread: Change colour [ It is possible in C ]?

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    93

    Change colour [ It is possible in C ]?

    Hi,

    Im making a CMD program and im looking to change the colour of certain pieces of text but not others. I have been able to change the whole window colour and the whole text colour by including the windows header and using the system command or something like that.

    All of the examples on the internet to change individual pieces of text in CMD either dont compile or do compile but dont work.

    Is it possible to do this, and if it is can you point in the direction of the commands that will allow me to do this.

    Regards,

    James

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sure you can use the Windows Console API to do that.
    Console Functions (Windows)

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    93
    Hi,

    Im sorry but that doesnt really make sense to me since im new to coding. Which console functions should I be using?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Depends on what you want to do... You can change a particular area's colour with:
    WriteConsoleOutputAttribute Function (Windows)
    You can write an area of a certain colour with:
    WriteConsoleOutput Function (Windows)

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    93
    This seems a lot more complicated than I first thought, although what I want to do is more simple than what you seem to be talking about. For a console program am I not able to do something like this:

    This is my text, I have both red and green colours.
    In HTML there is a function a bit like this <colour = red> test </colour>

    Is there not a simple C variation like this?

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    93
    This seems a lot more complicated than I first thought, although what I want to do is more simple than what you seem to be talking about. For a console program am I not able to do something like this:

    This is my text, I have both red and green colours.

    In HTML there is a function a bit like this <colour = red> test </colour>

    Is there not a simple C variation like this?

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by spadez View Post
    This seems a lot more complicated than I first thought,
    Guaranteed.

    In HTML there is a function a bit like this <colour = red> test </colour>

    Is there not a simple C variation like this?
    No. The reason being, there is some complicated C/C++ code that allows your web browser (which is written in C or C++) to behave this way.

    The "text console" may seem like a universal item in the sense that a text console is a text console, but then web browsers also seem that way: in reality, there are a number of different web browsers. If you refer to "the web browser" you are being slightly abstract; to get more specific you would have to say which web browser.

    A text console is the same kind of thing; it's a piece of software, and there are many more different kinds of consoles than there are web browsers. There is no universal scripting language (such as HTML) shared by all of them, and you must use an API built to interact with the console (such as the one suggested previously).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Well technically that's how it works under the surface. There are escape sequences that are sent to the terminal (monitor / command prompt) that it interprets as meaning things like colors, bolded text (on some terminals), etc... (edit: as MK27 mentioned - those sequences aren't consistent - that's why it has to be handled by a complicated library).

    I think ncurses (UNIX) is a lot simpler, you might like it - but if you're going to be developing for Windows, you have little other choice than the Microsoft APIs. Your compiler of course might supply a conio.h, which is usually a simplified API for making CLI's, but that won't be standardized.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It IS complicated. There is no easy way to do this - you somehow need to supply Windows with the information that it should use a different colour for different sections of the screen.

    You can use ANSI escape codes in Windows:
    ANSI escape code - Wikipedia, the free encyclopedia
    (Note you need to change your windows config.nt file, as described in the link for this to work).

    And there is, I think, pdcurses that will do ncurses like operations for Windows (and others).

    But eventually, that will translate to the Windows Console API.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I forgot about ANSI. You could easily assign those to string variables and use them in printf statements. For example (this is from a bash script, not C, but the ANSI codes are the same):
    Code:
    #bash prompt
    RED="\[\033[1;31m\]"
    GREEN="\[\033[0;32m\]"
    ORANGE="\[\033[0;33m\]"
    BLUE="\[\033[0;34m\]"
    MAUVE="\[\033[1;35m\]"
    CYAN="\[\033[1;36m\]"
    GRAY="\[\033[0;37m\]"
    hmm="\[\033[0;30m\]"
    hmm2="\[\033[0;40m\]"
    WHITE="\[\033[00m\]"
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    it depends on the OS and the compiler used...i remember the ancient TurboC++ IDE having color support ... they provided functions for rendering colored text in conio.h

    In UNIX-like OS any OS having curses.h library can also work with colors...by using its functions...

    I dont know about M$ Visual Studio and the libraries they provide...and I dont know what compiler and IDE and libraries you have access to...so its not possible to for me to help you...but it would be best for you to find what libraries you have with you and learn about the functions in those libraries...

    EDIT: or you can use ANSI codes,as given above^ that should work with any ANSI compliant Compiler...
    for example:
    Code:
    #include<stdio.h>
    
    int main()
    {
        printf("\E[48;31m\033[3mHello World\033[0m");
        return 0;
    }
    prints Hello World in Red on my screen, then goes back to the default color used in the terminal emulator. I m on GNU/Linux btw, so i dont know if its gonna work for you or not...cc in GNU/Linux is ANSI compliant so it works for me...if your complier is not ANSI compliant it wont work.
    Last edited by creeping death; 04-28-2009 at 11:12 AM.
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    ASNI escape codes has nothing to do with ANSI C (at least no more so than any other of the thousands of standards that ANSI has for various things) - it's to do with ANSI Video Terminal compatibility - in the old times, video terminals such as VT100 would use "Escape codes" to perform certain operations such as moving the cursor, clearing the screen, setting the colour of the text, etc.

    So it's not the compiler that needs to be ANSI compliant, but the output device (console window for example).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Linux emulates a VT-100 terminal, which uses certain escape codes - but those aren't necessarily the same escape codes used by the Windows command prompt. I'd almost bet that they're not.

  14. #14
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by sean View Post
    Linux emulates a VT-100 terminal, which uses certain escape codes - but those aren't necessarily the same escape codes used by the Windows command prompt. I'd almost bet that they're not.
    As far as I know, ANSI terminal codes are a superset of VT-100 codes, possibly with some weird incompatibilities, but for the most part one is a subset of the other.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  15. #15
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    However it would appear that Windows command prompts are not fully ANSI-compatible by default:

    ANSI escape code - Wikipedia, the free encyclopedia

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program wont run, always aborts
    By stormfront in forum C Programming
    Replies: 31
    Last Post: 10-31-2005, 05:55 PM
  2. Change buttons...
    By XunTric in forum C++ Programming
    Replies: 8
    Last Post: 09-27-2005, 08:22 AM
  3. To change text colour
    By Dirty Harry in forum C Programming
    Replies: 2
    Last Post: 06-16-2002, 03:04 AM
  4. Replies: 2
    Last Post: 09-04-2001, 02:12 PM